/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.popup;

import com.daikit.commons.shared.bean.AbstractDkBeanWithId;
import com.daikit.daikit4gxt.client.DkBaseMain;
import com.daikit.daikit4gxt.client.action.DkBaseAction;
import com.daikit.daikit4gxt.client.action.DkBaseAsyncCallback;
import com.sencha.gxt.data.shared.ListStore;


/**
 * Popup for deleting entity
 *
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$ * @param <BEANTYPE> deleted bean type
 * @param <BEANTYPE>
 *           the deleted bean type
 */
public abstract class DkYesNoForDeleteMessageBox<BEANTYPE extends AbstractDkBeanWithId> extends DkYesNoMessageBox
{

	protected BEANTYPE modelToDelete;
	private ListStore<? super BEANTYPE> store;
	private DkYesNoForDeleteMessageBoxCallback<BEANTYPE> callback;

	/**
	 * Constructor
	 */
	public DkYesNoForDeleteMessageBox()
	{
		super(null, null, MessageType.WARNING);
		setHeadingText(getLabelConfirmationTitle());
		setMessage(getLabelConfirmationMessage());
	}

	/**
	 * Show wrapper to {@link #show(ListStore, AbstractDkBeanWithId, DkYesNoForDeleteMessageBoxCallback)} with null
	 * callback.
	 *
	 * @param store
	 *           the {@link ListStore} for deleting the model afterwards (may be null)
	 * @param modelToDelete
	 *           the model to be deleted
	 */
	public void show(final ListStore<? super BEANTYPE> store, final BEANTYPE modelToDelete)
	{
		show(store, modelToDelete, null);
	}

	/**
	 * Show the deleting popup with callback
	 *
	 * @param modelToDelete
	 *           the model to be deleted
	 * @param callback
	 *           a {@link DkYesNoForDeleteMessageBoxCallback} (may be null)
	 */
	public void show(final BEANTYPE modelToDelete, final DkYesNoForDeleteMessageBoxCallback<BEANTYPE> callback)
	{
		show(null, modelToDelete, callback);
	}

	/**
	 * Show the deleting popup with callback
	 *
	 * @param store
	 *           the {@link ListStore} for deleting the model afterwards (may be null)
	 * @param modelToDelete
	 *           the model to be deleted
	 * @param callback
	 *           a {@link DkYesNoForDeleteMessageBoxCallback} (may be null)
	 */
	public void show(final ListStore<? super BEANTYPE> store, final BEANTYPE modelToDelete, final DkYesNoForDeleteMessageBoxCallback<BEANTYPE> callback)
	{
		this.modelToDelete = modelToDelete;
		this.store = store;
		this.callback = callback;
		super.show();
	}

	/**
	 * @return the model to be deleted (which have been passed to the message box via the show method
	 */
	public BEANTYPE getModelToDelete()
	{
		return modelToDelete;
	}

	@Override
	public void processYes()
	{
		getActionDelete().execute();
	}

	protected DkBaseAction<?> getActionDelete()
	{
		return new DkBaseAction<Void>(getLabelActionDeleteLoadingLabel())
		{
			@Override
			protected void run()
			{
				if (callback == null || callback.onBeforeDelete(modelToDelete))
				{
					callDeleteRpc(modelToDelete, new DkBaseAsyncCallback<Void>(this, true)
					{
						@Override
						public void processSuccess(final Void result)
						{
							DkMessageBox.info(getLabelPopupDeleteOkMessageBoxTitle(), getLabelPopupDeleteOkMessageBoxMessage(), new AfterCloseListener()
							{
								@Override
								public void afterClose()
								{
									if (getStore() != null)
									{
										getStore().remove(modelToDelete);
									}
									if (callback != null)
									{
										callback.onAfterDelete();
									}
								}
							});
						}
					});
				}
			}
		};
	}

	protected ListStore<? super BEANTYPE> getStore()
	{
		return this.store;
	}

	/**
	 * Please use {@link #show(ListStore, AbstractDkBeanWithId)} or
	 * {@link #show(ListStore, AbstractDkBeanWithId, DkYesNoForDeleteMessageBoxCallback)} instead of show()
	 */
	@Deprecated
	@Override
	public void show()
	{
		throw new RuntimeException(
				"Please use show(ListStore, AbstractDkBeanWithId) or show(ListStore, AbstractDkBeanWithId, DkYesNoForDeleteMessageBoxCallback) instead of show()");
	}

	/**
	 * @return the label for action delete progress bar
	 */
	protected String getLabelActionDeleteLoadingLabel()
	{
		return DkBaseMain.i18n().action_delete_loading_label(getBeanLabel());
	}

	/**
	 * @return the label for confirmation dialog box title
	 */
	protected String getLabelConfirmationTitle()
	{
		return DkBaseMain.i18n().popup_delete_ask_confirmation_title(getBeanLabel());
	}

	/**
	 * @return the label for confirmation dialog box message
	 */
	protected String getLabelConfirmationMessage()
	{
		return DkBaseMain.i18n().popup_delete_ask_confirmation_message(getBeanLabel());
	}

	/**
	 * @return the label for delete dialog box OK message box
	 */
	protected String getLabelPopupDeleteOkMessageBoxTitle()
	{
		return DkBaseMain.i18n().popup_delete_ok_title(getBeanLabel());
	}

	/**
	 * @return the label for delete dialog box OK message box message
	 */
	protected String getLabelPopupDeleteOkMessageBoxMessage()
	{
		return DkBaseMain.i18n().popup_delete_ok_message(getBeanLabel());
	}

	protected abstract void callDeleteRpc(BEANTYPE bean, DkBaseAsyncCallback<Void> callback);

	/**
	 * @return the bean label to be used in dialog box titles / messages / action progress bars ...
	 */
	protected abstract String getBeanLabel();

}
